﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shared.Model;
using Client;
using ManageSystem.Dialog;

namespace ManageSystem.Manager
{
    public partial class FormStation : UserControl
    {
        private List<STATION> _listSta = null;
        private STATION _choiceSta = null;
        public FormStation()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 清空信息
        /// </summary>
        private void ClearText()
        {
            this.textBoxStationMac.Clear();
            this.textBoxStationName.Clear();
            this.textBoxWorkerId.Clear();
            this.textBoxWorkerName.Clear();
            this.textBoxLineId.Clear();
            this.textBoxFlowId.Clear();
            this.textBoxNodeId.Clear();
            this.comboBoxStationType.Text = "";
            this.richTextBoxStaContext.Clear();
        }
        /// <summary>
        /// 设置Text信息
        /// </summary>
        /// <param name="sta"></param>
        private void SetText(STATION sta)
        {
            if (sta == null) return;

            this.textBoxStationMac.Text = sta.MAC;
            this.textBoxStationName.Text = sta.NAME;
            if (sta.WORKER_ID != null)
                this.textBoxWorkerId.Text = sta.WORKER_ID.ToString();
            this.textBoxWorkerName.Text = sta.WORKER_NAME;
            if (sta.LINE_ID != null)
                this.textBoxLineId.Text = sta.LINE_ID.ToString();
            if (sta.FLOW_ID != null)
                this.textBoxFlowId.Text = sta.FLOW_ID.ToString();
            if (sta.NODE_ID != null)
                this.textBoxNodeId.Text = sta.NODE_ID.ToString();
            this.comboBoxStationType.Text = sta.FLAG;
            this.richTextBoxStaContext.Text = sta.CONTEXT;
        }
        /// <summary>
        /// 将内容设置到sta中
        /// </summary>
        /// <param name="sta"></param>
        private void SetStation(STATION sta)
        {
            if (sta == null) return;

             sta.NAME=this.textBoxStationName.Text.Trim() ;

            long workerId = 0;
            if(long.TryParse(this.textBoxWorkerId.Text.Trim(),out workerId))
            {
                sta.WORKER_ID = workerId;
            }

             sta.WORKER_NAME= this.textBoxWorkerName.Text.Trim();

            long lineId = 0;
            if (long.TryParse(this.textBoxLineId.Text.Trim(),out lineId))
            {
                sta.LINE_ID = lineId;
            }

            long flowId = 0;
            if (long.TryParse(this.textBoxFlowId.Text.Trim(),out flowId))
            {
                sta.FLOW_ID = flowId;
            }

            long nodeId = 0;
            if (long.TryParse(this.textBoxNodeId.Text.Trim(),out nodeId))
            {
                sta.NODE_ID = nodeId;
            }

            sta.FLAG = this.comboBoxStationType.Text;
            sta.CONTEXT = this.richTextBoxStaContext.Text;
        }
        /// <summary>
        /// 设置List view
        /// </summary>
        /// <param name="view"></param>
        /// <param name="sta"></param>
        private void SetListViewStation(ListView view, STATION sta)
        {
            if (view == null || sta == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = sta.ID.ToString();
            item.SubItems.Add(sta.MAC);
            item.SubItems.Add(sta.NAME);

            if (sta.LINE_ID != null)
                item.SubItems.Add(sta.LINE_ID.ToString());
            else
                item.SubItems.Add("");

            if (sta.NODE_ID != null)
                item.SubItems.Add(sta.NODE_ID.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(sta.WORKER_NAME);

            view.Items.Add(item);
        }
        /// <summary>
        /// 清空list view
        /// </summary>
        /// <param name="view"></param>
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }
        /// <summary>
        /// 加载所有工位信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormStation_Load(object sender, EventArgs e)
        {
            ClearListView(this.listViewSta);
            ClearText();
            
            //加载所有的工位信息
            STATION staW = new STATION();
            var selectSta = MethodWcf.SelectStationClient(staW);
            if (selectSta.result)
            {
                _listSta = selectSta.value;
                for(int i = 0; i < _listSta.Count; i++)
                {
                    SetListViewStation(this.listViewSta, _listSta[i]);
                }

                //添加工位类型信息

                this.comboBoxStationType.Items.Clear();

                var listFlag = (from s in _listSta
                                select s.FLAG).Distinct().ToList();

                if (listFlag.Count == 0) return;

                for (int j = 0; j < listFlag.Count; j++)
                {
                    this.comboBoxStationType.Items.Add(listFlag[j]);
                }

            }
        }
        /// <summary>
        /// 选择工位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listViewSta_MouseClick(object sender, MouseEventArgs e)
        {
            ClearText();
            if (this._listSta.Count == 1)//只有一条选项
            {
                _choiceSta = _listSta[0];
                SetText(_choiceSta);
            }

            var selected = this.listViewSta.SelectedItems;
            if (this.listViewSta.Items.Count >= 1 && selected.Count == 1)//ITEM的数量大于1
            {
                _choiceSta = (from f in _listSta
                                 where f.ID == long.Parse(selected[0].Text)
                                 select f).FirstOrDefault();

                SetText(_choiceSta);
            }
        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonChg_Click(object sender, EventArgs e)
        {
            _choiceSta.SetUpdate();
            SetStation(_choiceSta);

            _choiceSta.SetWhere();
            _choiceSta.ID = _choiceSta.ID;

            var updateSta = MethodWcf.UpdateStationClient(_choiceSta);
            MessageBox.Show(updateSta.message);

            _choiceSta = null;
            FormStation_Load(null, null);//重新加载内容
        }
        /// <summary>
        /// 员工ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxWorkerId_Enter(object sender, EventArgs e)
        {
            DialogWorker worker = new DialogWorker(this.textBoxWorkerId, this.textBoxWorkerName);
            worker.ShowDialog();
        }
        /// <summary>
        /// 产线ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxLineId_Enter(object sender, EventArgs e)
        {
            DialogLine line = new DialogLine(this.textBoxLineId, this.textBoxFlowId);
            line.ShowDialog();
        }
        /// <summary>
        /// 工序ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxNodeId_Enter(object sender, EventArgs e)
        {
            if (_choiceSta == null || _choiceSta.FLOW_ID == null)
            {
                MessageBox.Show("请选择产线");
                return;
            }

            NODE nodeW = new NODE();
            nodeW.SetWhere();
            nodeW.FLOW_ID = _choiceSta.FLOW_ID;
            DialogNode node = new DialogNode(this.textBoxNodeId, nodeW);
            node.ShowDialog();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
             //①搜索满足条件的行
            //②将相关的行显示在窗口中
            ClearListView(this.listViewSta);//清空链表
            string searchText = this.textBoxSearch.Text.Trim();
            if (string.IsNullOrEmpty(searchText))//如果没有搜索条件则将所有的信息加载进来
            {
                for (int i = 0; i < _listSta.Count; i++)
                {
                    SetListViewStation(this.listViewSta, _listSta[i]);
                }
                return;
            }

            var listSearch = (from n in _listSta
                              where string.Format("{0}|{1}|{2}|{3}|{4}|{5}",
                              n.ID,
                              n.MAC,
                              n.NAME,
                              n.LINE_ID,
                              n.NODE_ID,
                              n.WORKER_NAME)
                              .Contains(searchText)
                              select n).ToList();

            if (listSearch == null || listSearch.Count == 0) return;

            //给List View赋值
            for (int i = 0; i < listSearch.Count; i++)
            {
                SetListViewStation(this.listViewSta, listSearch[i]);
            }
        }
    }
}
